package inabit.data.expression.arithmetic;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import inabit.base.WorkingMemory;
/**@author Rafael Rocha*/
public class SubExpression implements ArithmeticExpression{
	private ArithmeticExpression left;
	private ArithmeticExpression right;
	
	public SubExpression(ArithmeticExpression left, ArithmeticExpression right) {
		this.left = left;
		this.right = right;
	}
	@Override
	public String getResult(WorkingMemory wm) {
		String leftStr = left.getResult(wm);
		String rightStr = right.getResult(wm);
		if(leftStr == null || rightStr == null) {return null;}
		Double lVal = Double.parseDouble(leftStr);
		Double rVal = Double.parseDouble(rightStr);
		return Double.toString(lVal - rVal);
	}

	@Override
	public String stringForm() {
		String leftStr = left.stringForm();
		String rightStr = right.stringForm();
		if(!left.isLeaf()) {leftStr = "( " + leftStr + " )";}
		if(!right.isLeaf()) {rightStr = "( " + rightStr + " )";}
		return leftStr + " - " + rightStr;
	}
	@Override
	public Collection<String> getReferenced() {
		List<String> all = new ArrayList<String>();
		all.addAll(left.getReferenced());
		all.addAll(right.getReferenced());
		return all;
	}
	@Override
	public boolean isLeaf() {
		return false;
	}
}
